package com.haonan.springboot_exercise.service.impl;

import com.alibaba.fastjson.JSON;
import com.haonan.springboot_exercise.SpringbootExerciseApplication;
import com.haonan.springboot_exercise.bean.pojo.MsgResult;
import com.haonan.springboot_exercise.bean.pojo.Result;
import com.haonan.springboot_exercise.bean.pojo.ServicePort;
import com.haonan.springboot_exercise.dao.PopServerDao;
import com.haonan.springboot_exercise.netty.client.PopClient;
import com.haonan.springboot_exercise.netty.server.PopServer;
import com.haonan.springboot_exercise.netty.server.SmtpServer;
import com.haonan.springboot_exercise.service.PopServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * @author haonan
 * @date 2021/4/17 21:12
 **/
@Component
public class PopServerImpl implements PopServerService {
    @Autowired
    PopServerDao popServerDao;

    /**
     *
     * @param payload email_id集合
     * @return
     */
    @Override
    public String getLISTResult(String payload) {
        return JSON.toJSONString(new MsgResult(250,"LISTReady"));
    }

    @Override
    public Result closePopServer() throws Exception {
        PopClient popClient = new PopClient();
        popClient.run();
        popClient.getPopClientHandler().sendDataToServer(popClient.getChannel(), "CLOSE_POP");
        popServerDao.setState(false);
        return Result.succ(null);
    }

    /**
     * 开启Pop服务
     *
     * @return
     * @throws Exception
     */
    @Override
    public Result openPopServer() throws Exception {
        new Thread(()->{
            PopServer popServer = new PopServer();
            popServer.start(new InetSocketAddress("127.0.0.1", popServerDao.getPopPort("127.0.0.1")));
        }).start();
        popServerDao.setState(true);
        return Result.succ(null);
    }
}
